package org.jeese.common.config;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
	private static final Logger logger = LoggerFactory.getLogger(SwaggerConfig.class);
	
	@Bean
	public Docket appApi() {
		logger.info("init SwaggerConfig");
		
		ApiInfo apiInfo = new ApiInfoBuilder()  
				.title("springboot利用swagger构建api文档")
				.description("简单优雅的restfun风格")
				.termsOfServiceUrl("域名url")
				.contact(new Contact("jeese","xxx.com","xx@126.com"))
				.version("1.0")
				.build();
		
		//可以添加多个header或参数
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder
                .parameterType("header") //参数类型支持header, cookie, body, query etc
                .name("token") //参数名
                .defaultValue("token") //默认值
                .description("header中token字段测试")
                .modelRef(new ModelRef("string"))//指定参数值的类型
                .required(false)//非必需，这里是全局配置，然而在登陆的时候是不用验证的
                .build();
        List<Parameter> parameters = new ArrayList<Parameter>();
        parameters.add(aParameterBuilder.build());
		
		Docket docker = new Docket(DocumentationType.SWAGGER_2)
				.groupName("app")
				.select()
				//设置只生成被Api这个注解注解过的Ctrl类中有ApiOperation注解的api接口的文档
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				//.apis(RequestHandlerSelectors.basePackage("org.jeese.modules"))
				//.paths(Predicates.or(PathSelectors.regex("/api/v1/.*")))//过滤的接口
				.paths(PathSelectors.ant("/api/v1/**"))//过滤的接口
				//.paths(PathSelectors.any())//匹配全部
				.build()
				.apiInfo(apiInfo);
				//.globalOperationParameters(parameters);
		return docker;
	}
	
	@Bean
	public Docket adminApi() {
		ApiInfo apiInfo = new ApiInfoBuilder()  
				.title("springboot利用swagger构建api文档")
				.description("简单优雅的restfun风格")
				.termsOfServiceUrl("域名url")
				.contact(new Contact("jeese","xxx.com","xx@126.com"))
				.version("1.0")
				.build();
		
		//可以添加多个header或参数
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder
                .parameterType("header") //参数类型支持header, cookie, body, query etc
                .name("token") //参数名
                .defaultValue("token") //默认值
                .description("header中token字段测试")
                .modelRef(new ModelRef("string"))//指定参数值的类型
                .required(false)//非必需，这里是全局配置，然而在登陆的时候是不用验证的
                .build(); 
        List<Parameter> parameters = new ArrayList<Parameter>();
        parameters.add(aParameterBuilder.build());
		
		Docket docker = new Docket(DocumentationType.SWAGGER_2)
				.groupName("admin")
				.select()
				//设置只生成被Api这个注解注解过的Ctrl类中有ApiOperation注解的api接口的文档
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				//.apis(RequestHandlerSelectors.basePackage("org.jeese.modules"))
				//.paths(Predicates.or(PathSelectors.regex("/api/v1/.*")))//过滤的接口
				.paths(PathSelectors.ant("/api/admin/**"))//过滤的接口
				//.paths(PathSelectors.any())//匹配全部
				.build()
				.apiInfo(apiInfo);
				//.globalOperationParameters(parameters);
		return docker;
	}
}